import {
  Controller,
  Get,
  Post,
  Delete,
  Body,
  Param,
  Query,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { MarkService } from './mark.service';
import { CreateMarkDto, QueryMarkDto } from './dto';
import { ApiResponse } from '../../common/interfaces/api-response.interface';
import {
  MarkStats,
  RestaurantMarkCheck,
} from '../../common/types/response.types';
import { RestaurantMark } from '../../entities/restaurant-mark.entity';
import type { MarkType } from '../../entities/restaurant-mark.entity';

@Controller('marks')
export class MarkController {
  constructor(private readonly markService: MarkService) {}

  // 创建标记
  @Post()
  async createMark(@Body() createMarkDto: CreateMarkDto) {
    const data = await this.markService.createMark(createMarkDto);
    return {
      success: true,
      data,
      message: '标记创建成功',
    } as ApiResponse<RestaurantMark>;
  }

  // 标记今天不想吃
  @Post('today/:restaurantId')
  async markToday(@Param('restaurantId') restaurantId: string) {
    const data = await this.markService.markToday(restaurantId);
    return {
      success: true,
      data,
      message: '今天标记成功',
    } as ApiResponse<RestaurantMark>;
  }

  // 标记本周不想吃
  @Post('week/:restaurantId')
  async markWeek(@Param('restaurantId') restaurantId: string) {
    const data = await this.markService.markWeek(restaurantId);
    return {
      success: true,
      data,
      message: '本周标记成功',
    } as ApiResponse<RestaurantMark>;
  }

  // 获取所有标记
  @Get()
  async getAllMarks(@Query() query: QueryMarkDto) {
    const data = await this.markService.getAllMarks(query);
    return {
      success: true,
      data,
    } as ApiResponse<typeof data>;
  }

  // 获取活跃标记
  @Get('active')
  async getActiveMarks() {
    const data = await this.markService.getActiveMarks();
    return {
      success: true,
      data,
    } as ApiResponse<RestaurantMark[]>;
  }

  // 获取今天的标记
  @Get('today')
  async getTodayMarks() {
    const data = await this.markService.getTodayMarks();
    return {
      success: true,
      data,
    } as ApiResponse<RestaurantMark[]>;
  }

  // 获取本周的标记
  @Get('week')
  async getWeekMarks() {
    const data = await this.markService.getWeekMarks();
    return {
      success: true,
      data,
    } as ApiResponse<RestaurantMark[]>;
  }

  // 获取标记统计
  @Get('stats')
  async getMarkStats() {
    const data = await this.markService.getMarkStats();
    return {
      success: true,
      data,
    } as ApiResponse<MarkStats>;
  }

  // 获取指定餐厅的标记
  @Get('restaurant/:restaurantId')
  async getRestaurantMarks(@Param('restaurantId') restaurantId: string) {
    const data = await this.markService.getRestaurantMarks(restaurantId);
    return {
      success: true,
      data,
    } as ApiResponse<RestaurantMark[]>;
  }

  // 检查餐厅是否被标记
  @Get('check/:restaurantId')
  async checkRestaurantMark(
    @Param('restaurantId') restaurantId: string,
    @Query('markType') markType?: MarkType,
  ) {
    const data = await this.markService.checkRestaurantMark(
      restaurantId,
      markType,
    );
    return {
      success: true,
      data,
    } as ApiResponse<RestaurantMarkCheck>;
  }

  // 删除标记
  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  async deleteMark(@Param('id') id: string) {
    await this.markService.deleteMark(id);
    return {
      success: true,
      data: null,
      message: '标记删除成功',
    } as ApiResponse<null>;
  }

  // 删除指定餐厅的标记
  @Delete('restaurant/:restaurantId')
  @HttpCode(HttpStatus.NO_CONTENT)
  async deleteRestaurantMarks(
    @Param('restaurantId') restaurantId: string,
    @Query('markType') markType?: MarkType,
  ) {
    await this.markService.deleteRestaurantMarks(restaurantId, markType);
    return {
      success: true,
      data: null,
      message: '餐厅标记删除成功',
    } as ApiResponse<null>;
  }

  // 删除特定类型的标记
  @Delete('type/:markType')
  @HttpCode(HttpStatus.NO_CONTENT)
  async deleteMarksByType(@Param('markType') markType: MarkType) {
    await this.markService.deleteMarksByType(markType);
    return {
      success: true,
      data: null,
      message: `${markType === 'today' ? '今天' : '本周'}标记删除成功`,
    } as ApiResponse<null>;
  }

  // 清空所有标记
  @Delete('clear/all')
  @HttpCode(HttpStatus.NO_CONTENT)
  async clearAllMarks() {
    await this.markService.clearAllMarks();
    return {
      success: true,
      data: null,
      message: '所有标记清空成功',
    } as ApiResponse<null>;
  }

  // 清空过期标记
  @Delete('clear/expired')
  @HttpCode(HttpStatus.NO_CONTENT)
  async clearExpiredMarks() {
    await this.markService.clearExpiredMarks();
    return {
      success: true,
      data: null,
      message: '过期标记清空成功',
    } as ApiResponse<null>;
  }
}
